//
// CrackLocatorReader
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.core;

import java.io.*;
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class CrackLocatorReader extends DefaultHandler
{
    // ============================================================
    // Enums
    // ============================================================

    public enum State
    {
        crackLocator( "crack-locator" );

        private String tag;

        State( String tag )
        {
            this.tag = tag;
        }

        public String tag()
        {
            return tag;
        }
    }

    // ============================================================
    // Fields
    // ============================================================

    private Stack<State> stateStack;
    private File dir;

    private File buildFileDir;

    // ============================================================
    // Constructors
    // ============================================================

    public CrackLocatorReader()
    {
        buildFileDir = null;
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void parse( File file )
        throws SAXException, IOException
    {
        stateStack = new Stack<State>();
        dir = file.getParentFile();

        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setErrorHandler( new ReaderErrorHandler() );
        reader.setContentHandler( this );
        reader.parse( new InputSource( new FileReader( file ) ) );
    }

    public File getBuildFileDir()
    {
        return buildFileDir;
    }

    // ----------
    // protected
    // ----------

    @Override
    public void startDocument()
        throws SAXException
    {
    }

    @Override
    public void startElement( String uri, String tagName, String qName, Attributes atts )
        throws SAXException
    {
        State newState = null;

        if ( tagName.equalsIgnoreCase( State.crackLocator.tag() ) )
        {
            newState = State.crackLocator;
            String path = atts.getValue( "path" );
            buildFileDir = new File( path );
            if ( !buildFileDir.exists() )
            {
                buildFileDir = new File( dir, path );
            }
        }

        pushState( newState );
    }

    @Override
    public void characters( char[] value, int offset, int count )
        throws SAXException
    {
    }

    @Override
    public void endElement( String uri, String tagName, String qName )
        throws SAXException
    {
    }

    @Override
    public void endDocument()
        throws SAXException
    {
    }

    // ----------
    // protected
    // ----------

    protected State getState()
    {
        if ( stateStack.isEmpty() )
        {
            return null;
        }

        return stateStack.peek();
    }

    protected State popState()
    {
        State state = null;

        stateStack.pop();
        if ( !stateStack.isEmpty() )
        {
            state = stateStack.peek();
        }

        return state;
    }

    protected void pushState( State state )
    {
        stateStack.push( state );
    }

    // ============================================================
    // Inner Classes
    // ============================================================

    public class ReaderErrorHandler implements ErrorHandler
    {
        @Override
        public void warning( SAXParseException ex )
            throws SAXException
        {
            throw ex;
        }

        @Override
        public void error( SAXParseException ex )
            throws SAXException
        {
            throw ex;
        }

        @Override
        public void fatalError( SAXParseException ex )
            throws SAXException
        {
            throw ex;
        }
    }
}
